﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ProgramMain.Database;
using ProgramMain.Framework;

namespace ProgramMain.OpticNet
{
    public partial class EquipmentList : ProgramMain.Framework.BaseGridListCtl
    {
        public EquipmentList()
        {
            InitializeComponent();
        }

        private Nullable<Int32> _equipID;
        /// <summary>
        /// Фильтр по объекту
        /// </summary>
        public Nullable<Int32> EquipID
        {
            get { return _equipID; }
            set
            {
                if (_equipID != value)
                {
                    _equipID = value;

                }
            }
        }

        protected override bool IsLoadDelayed()
        {
            return !(DetailID.HasValue || EquipID.HasValue);
        }

        protected override void LoadData()
        {
            //прочитать данные из базы и заполнить датасет
            this.equipmentTableAdapter.SelectCommandTimeout = ProgramMain.Properties.Settings.Default.LongQueryTimeout;
            if (!DetailID.HasValue && !EquipID.HasValue)
            {
                simpleOpticNetDB.Fibre.Clear();
            }
            else
            {
                this.equipmentTableAdapter.Fill(this.simpleOpticNetDB.Equipment, DetailID == 0 ? null : DetailID, EquipID);
            }
            UpdateStatusText(this.simpleOpticNetDB.Equipment);
        }

        override protected BaseModalDetailCtl CreateDetailDialog(BaseModalDetailCtl.ShowModeTypes pShowMode)
        {
            return new EquipmentDetail(){VertexID = DetailID};
        }

        protected override void UpdateData()
        {
            //внести изменения в базу
            this.equipmentTableAdapter.SetUpdateTimeout = ProgramMain.Properties.Settings.Default.LongQueryTimeout;
            this.equipmentTableAdapter.Update(this.simpleOpticNetDB.Equipment);
            UpdateStatusText(this.simpleOpticNetDB.Equipment);
        }

        private void EquipmentList_ViewRowIDChanged(object sender, ShowDetailEventArgs e)
        {

        }

        private void EquipmentList_DetailIDChanged(object sender, DetailEventArgs e)
        {
            DBRead();
        }

        protected override void biDetails_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            switch (Present)
            {
                case PresentFormType.InVertexes:
                    if (Framework.BaseSecurity.CheckSecurityViewAction())
                    {
                        FrmEquipment frmEquipment = FrmEquipment.ShowForm(false);
                        frmEquipment.SetActiveEquip(ViewRowID.Value);
                    }
                    break;
                case PresentFormType.InEquipment:
                    if (Framework.BaseSecurity.CheckSecurityViewAction())
                    {
                        FrmVertexes frmVertexes = FrmVertexes.ShowForm();
                        if (dataSource.Current != null)
                        {
                            SimpleOpticNetDB.EquipmentRow row =
                                (SimpleOpticNetDB.EquipmentRow)((DataRowView)dataSource.Current).Row;
                            frmVertexes.SetActiveVertex(row.VertexID);
                            frmVertexes.SetTableActive(Present);
                        }
                    }
                    break;
            }
        }

        protected override void biConnection_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (dataSource.Current == null) return;
            SimpleOpticNetDB.EquipmentRow equipmentRow = (SimpleOpticNetDB.EquipmentRow)((DataRowView)dataSource.Current).Row;

            FrmFibrePointDetail fibrePointDetail = new FrmFibrePointDetail();
            fibrePointDetail.VertexID = equipmentRow.VertexID;
            fibrePointDetail.VertexName = string.Format("{0}", equipmentRow.Vertex);
            fibrePointDetail.EquipVertexID = equipmentRow.ID;
            fibrePointDetail.ShowForm();
        }

        protected override void biVertexMap_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (dataSource.Current == null) return;
            try
            {
                SimpleOpticNetDB.EquipmentRow equipmentRow = (SimpleOpticNetDB.EquipmentRow)((DataRowView)dataSource.Current).Row;
                FrmVertexMap.ShowForm(false, equipmentRow.VertexID);
            }
            catch (Exception)
            {

            }
        }

        public int? AddEquipment(int vertexID)
        {
            if (!CheckSecurityEditAction()) return null;

            dataSource.MoveLast();
            EquipmentDetail dialog = new EquipmentDetail();
            dialog.VertexID = vertexID;
            dialog.dataSource = dataSource;
            dialog.dataSource.AddNew();
            try
            {

                SimpleOpticNetDB.EquipmentRow row =
                    (SimpleOpticNetDB.EquipmentRow)((DataRowView)dialog.dataSource.Current).Row;
                row.VertexID = vertexID;
                row.ModuleCount = 1;
                row.FibreCount = 1;
                row.CurveElementTypeID = 28;
                row.UserID = ProgramMain.Framework.BaseSecurity.UserID;
                DialogResult dlgRes = dialog.ShowModal(BaseModalDetailCtl.ShowModeTypes.Add);
                if (dlgRes == DialogResult.OK && dialog.CheckValidation())
                {
                    equipmentTableAdapter.Update(row);
                    return row.ID;
                }
            }
            catch (Exception)
            {
                XtraMessageBox.Show("Произошла ошибка при добавлении кросса в колодец.", "Ошибка", MessageBoxButtons.OK);

            }
            return null;
        }

        public void DeleteEquipment()
        {
            Action_Delete(this, EventArgs.Empty);
        }

        protected override void biAddDocument_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (dataSource.Current != null)
            {
                SimpleOpticNetDB.EquipmentRow row = (SimpleOpticNetDB.EquipmentRow)((DataRowView)dataSource.Current).Row;
                new DocumentList() { CurveElementID = row.ID }.AddDocument();

            }
        }

        protected override void biDocumentList_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (dataSource.Current != null)
            {
                SimpleOpticNetDB.EquipmentRow row = (SimpleOpticNetDB.EquipmentRow)((DataRowView)dataSource.Current).Row;
                new FrmDocumentList() { CurveElementID = row.ID }.ShowDialog();
            }
        }
    }
}
